/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.generator;

/**
 *
 * @author Laurent WOUTERS
 */
public class PackageGenerator {
    protected ModelGenerator modelGenerator;
    protected xowl.lang.owl2.Ontology ontology;
    protected String packageName;
    protected java.util.Map<xowl.lang.runtime.Class, ClassGenerator> classes;
    protected java.util.Map<xowl.lang.runtime.Class, ClassGenerator> anonymousClasses;
    protected java.util.Map<xowl.lang.runtime.Property, PropertyGenerator> properties;

    public ModelGenerator getModel() { return modelGenerator; }
    public xowl.lang.owl2.Ontology getOWLOntology() { return ontology; }
    public String getName() { return packageName; }
    public String getFullName() { return modelGenerator.getBasePackage() + "." + packageName; }
    public ClassGenerator getClass(xowl.lang.runtime.Class owlClass) { return classes.get(owlClass); }
    public java.util.Collection<ClassGenerator> getClasses() { return classes.values(); }
    public java.util.Collection<ClassGenerator> getAnonymousClasses() { return anonymousClasses.values(); }
    public PropertyGenerator getProperty(xowl.lang.runtime.Property owlProperty) { return properties.get(owlProperty); }
    public java.util.Collection<PropertyGenerator> getProperties() { return properties.values(); }
    
    public PackageGenerator(Application app, ModelGenerator model, xowl.lang.owl2.Ontology ontology, String name) {
        this.modelGenerator = model;
        this.ontology = ontology;
        this.packageName = name.toLowerCase();
        this.classes = new java.util.HashMap<xowl.lang.runtime.Class, ClassGenerator>();
        this.anonymousClasses = new java.util.HashMap<xowl.lang.runtime.Class, ClassGenerator>();
        this.properties = new java.util.HashMap<xowl.lang.runtime.Property, PropertyGenerator>();
        loadEntities(app);
    }
    protected void loadEntities(Application app) {
        for (xowl.lang.runtime.Entity entity : ontology.getAllContains()) {
            for (xowl.lang.runtime.Interpretation interpretation : entity.getAllInterpretedAs()) {
                if (interpretation instanceof xowl.lang.runtime.Class) {
                    xowl.lang.runtime.Class c = (xowl.lang.runtime.Class)interpretation;
                    classes.put(c, new ClassGenerator(app, this, c));
                } else if (interpretation instanceof xowl.lang.runtime.Property) {
                    xowl.lang.runtime.Property prop = (xowl.lang.runtime.Property)interpretation;
                    properties.put(prop, new PropertyGenerator(app, this, prop));
                }
            }
        }
    }

    public void addAnonymousClass(xowl.lang.runtime.Class c, ClassGenerator gen) { anonymousClasses.put(c, gen); }

    public void write(String folder, GenerationMode mode, int signature) throws java.io.IOException {
        if ((classes.size() + anonymousClasses.size()) == 0)
            return;
        String myFolder = folder + packageName + "/";
        java.io.File directory = new java.io.File(myFolder);
        directory.mkdir();
        for (ClassGenerator gen : classes.values())
            gen.write(myFolder, mode, signature);
        for (ClassGenerator gen : anonymousClasses.values())
            gen.write(myFolder, mode, signature);
    }
}
